<?php

namespace App\Console\Commands;

use App\Models\AppointNum;
use App\Models\Hospital;
use App\Console\Commands\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\FamilyType;
use App\Models\HospitalAppointType;
use App\Models\old\HospitalAppoint;
use App\Models\old\HospitalAppointStreet;
use App\Models\old\HospitalAppointVaccine;
use App\Models\old\HospitalAppointVaccineTimeNum;
use App\Models\old\HospitalAppointWeek;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Street;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class migrationAppoint extends Command
{
    public static $cycleNum = [1 => 7, 2 => 14, 3 => 30, 4 => 1, 5 => 2, 6 => 3, 7 => 4, 8 => 5, 9 => 60, 10 => 90, 11 => 120];

    public static $rtText = [
        0 => '00:00',
        5 => '05:00',
        6 => '06:00',
        7 => '07:00',
        11 => '11:00',
        12 => '12:00',
        13 => '13:00',
        14 => '14:00',
        15 => '15:00',
        16 => '16:00',
        17 => '17:00',
        18 => '18:00',
        19 => '19:00',
        20 => '20:00',
        21 => '21:00',
        22 => '22:00',
        23 => '23:00',
    ];
    public static $timeText = [
        1 => [
            '1' => '08:00-09:00',
            '2' => '09:00-10:00',
            '3' => '10:00-11:00',
            '4' => '13:00-14:00',
            '5' => '14:00-15:00',
            '6' => '15:00-15:31',
        ],
        2 => [
            '7' => '08:00-08:30',
            '8' => '08:30-09:00',
            '9' => '09:00-09:30',
            '10' => '09:30-10:00',
            '11' => '10:00-10:30',
            '12' => '10:30-11:00',
            '19' => '11:00-11:30',
            '20' => '11:30-12:00',
            '13' => '13:00-13:30',
            '14' => '13:30-14:00',
            '15' => '14:00-14:30',
            '16' => '14:30-15:00',
            '17' => '15:00-15:30',
            '18' => '15:30-16:00'
        ],
    ];
    public static $timeText1 = [
        '1' => '08:00-09:00',
        '2' => '09:00-10:00',
        '3' => '10:00-11:00',
        '4' => '13:00-14:00',
        '5' => '14:00-15:00',
        '6' => '15:00-15:31',
        '7' => '08:00-08:30',
        '8' => '08:30-09:00',
        '9' => '09:00-09:30',
        '10' => '09:30-10:00',
        '11' => '10:00-10:30',
        '12' => '10:30-11:00',
        '19' => '11:00-11:30',
        '20' => '11:30-12:00',
        '13' => '13:00-13:30',
        '14' => '13:30-14:00',
        '15' => '14:00-14:30',
        '16' => '14:30-15:00',
        '17' => '15:00-15:30',
        '18' => '15:30-16:00'
    ];
    public static $amPmArr = [
        1 => [
            '1' => '08:00-09:00',
            '2' => '09:00-10:00',
            '3' => '10:00-11:00',
            '7' => '08:00-08:30',
            '8' => '08:30-09:00',
            '9' => '09:00-09:30',
            '10' => '09:30-10:00',
            '11' => '10:00-10:30',
            '12' => '10:30-11:00',
            '19' => '11:00-11:30',
            '20' => '11:30-12:00',
        ],
        2 => [
            '4' => '13:00-14:00',
            '5' => '14:00-15:00',
            '6' => '15:00-15:31',
            '13' => '13:00-13:30',
            '14' => '13:30-14:00',
            '15' => '14:00-14:30',
            '16' => '14:30-15:00',
            '17' => '15:00-15:30',
            '18' => '15:30-16:00'
        ]
    ];
    public static $typeText = [
        1 => 1,
        2 => 2,
        8 => 3,
        4 => 13,
        5 => 4,
        6 => 5,
        3 => 6,
        7 => 7,
        9 => 8,
        10 => 9,
        11 => 10,
        12 => 11,
        13 => false,
    ];

    protected static $month = [
        1 => [
            ['month' => 1, 'month_name' => '满月', 'is_check' => 0],
        ],
        2 => [
            ['month' => 2, 'month_name' => '2个月', 'is_check' => 0],
            ['month' => 3, 'month_name' => '3个月', 'is_check' => 0],
        ],
        3 => [
            ['month' => 5, 'month_name' => '5个月', 'is_check' => 0],
            ['month' => 6, 'month_name' => '6个月', 'is_check' => 0],

        ],
        4 => [
            ['month' => 8, 'month_name' => '8个月', 'is_check' => 0],
            ['month' => 9, 'month_name' => '9个月', 'is_check' => 0],

        ],
        5 => [
            ['month' => 11, 'month_name' => '11个月', 'is_check' => 0],
            ['month' => 12, 'month_name' => '12个月', 'is_check' => 0],

        ],
        6 => [
            ['month' => 17, 'month_name' => '1岁5个月', 'is_check' => 0],
            ['month' => 18, 'month_name' => '1岁6个月', 'is_check' => 0],
        ],
        7 => [
            ['month' => 23, 'month_name' => '1岁11个月', 'is_check' => 0],
            ['month' => 24, 'month_name' => '1岁11个月', 'is_check' => 0],
        ],
        8 => [
            ['month' => 29, 'month_name' => '2岁5个月', 'is_check' => 0],
            ['month' => 30, 'month_name' => '2岁6个月', 'is_check' => 0],
        ],
        9 => [
            ['month' => 35, 'month_name' => '2岁11个月', 'is_check' => 0],
            ['month' => 36, 'month_name' => '2岁12个月', 'is_check' => 0],
            ['month' => 37, 'month_name' => '3岁1个月', 'is_check' => 0],
            ['month' => 38, 'month_name' => '3岁2个月', 'is_check' => 0],
            ['month' => 39, 'month_name' => '3岁3个月', 'is_check' => 0],
            ['month' => 40, 'month_name' => '3岁4个月', 'is_check' => 0],
            ['month' => 41, 'month_name' => '3岁5个月', 'is_check' => 0],
            ['month' => 42, 'month_name' => '3岁6个月', 'is_check' => 0],
            ['month' => 43, 'month_name' => '3岁7个月', 'is_check' => 0],
            ['month' => 44, 'month_name' => '3岁8个月', 'is_check' => 0],
            ['month' => 45, 'month_name' => '3岁9个月', 'is_check' => 0],
            ['month' => 46, 'month_name' => '3岁10个月', 'is_check' => 0],
            ['month' => 47, 'month_name' => '3岁11个月', 'is_check' => 0],
            ['month' => 48, 'month_name' => '3岁12个月', 'is_check' => 0],
        ],
    ];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:appoint';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
//        $sql = "SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));";
//        DB::statement($sql);
//        echo "基础设置开始配置\n";
//        echo date('Y-m-d H:i:s')."\n";
//        $hospitalAppoints = HospitalAppoint::query()->get();
//        foreach($hospitalAppoints as $hospitalAppoint){
//            $row = [];
//            $row['id'] = $hospitalAppoint->id;
//            $haids[] = $hospitalAppoint->id;
//            $row['hospital_id'] = $hospitalAppoint->doctorid;
//            $row['weeks'] = json_encode(str_split((string)$hospitalAppoint->weeks));
//            $row['type'] = self::$typeText[$hospitalAppoint->type];
//            if(!$row['type'])
//              continue;
//            $row['cycle'] = self::$cycleNum[$hospitalAppoint->cycle];
//            $row['delay'] = $hospitalAppoint->delay;
//            $row['sure_date'] = json_encode(explode(',',$hospitalAppoint->sure_date));
//            $row['non_date'] = json_encode(explode(',',$hospitalAppoint->non_date));
//            $row['is_month'] = $hospitalAppoint->is_month;
//            $row['release_time'] = self::$rtText[$hospitalAppoint->release_time];
//            $row['intro'] = $hospitalAppoint->info;
//            $row['phone'] = $hospitalAppoint->phone;
////            $ordHospital = OldHospital::query()->where('userid',$hospitalAppoint->doctorid)->first();
////            if($ordHospital){
////                if(strpos($ordHospital->appoint,',')!==false){
////                    $appoints = explode(',',$ordHospital->appoint);
////                }elseif ($ordHospital->appoint) {
////                    $appoints = str_split((string)$ordHospital->appoint);
////                }else{
////                    $appoints = [];
////                }
////
////                $row['is_open'] = isset($appoints) && in_array($hospitalAppoint->type,$appoints)?1:0;
////            }
//            $row['is_open'] = 0;
//            //echo $hospitalAppoint->id."--".$row['is_open']."---".$ordHospital->appoint;
//            echo "\n";
//            \App\Models\HospitalAppoint::query()->updateOrCreate(['id'=>$hospitalAppoint->id],$row);
//        }
//        echo "基础设置配置结束\n";
//        echo "街道导入\n";
//
//        $streets = Street::query()->get();
//        $row = [];
//        $data = [];
//        foreach ($streets as $street) {
//
//            $row = $street->toArray();
//            $row['hospital_id'] = $row['doctorid'];
//            unset($row['doctorid']);
//            $data[] = $row;
//        }
//        \App\Models\Street::query()->upsert($data, ['id']);
//        //预约时间段导入
//        echo "预约时间段导入\n";
//
//         $hospitalAppoints = HospitalAppoint::query()->get();
//         $row = [];
//         $data = [];
//         foreach($hospitalAppoints as $hospitalAppoint){
//             $newHospitalAppoint = \App\Models\HospitalAppoint::query()->where('id',$hospitalAppoint->id)->first();
//             if (!$newHospitalAppoint)
//                 continue;
//             foreach(self::$timeText as $tk=>$tv){
//                 foreach($tv as $tvk=>$tvv){
//                     //echo $tvv;echo "\n";
//
//                     $time = explode('-',$tvv);
//                     list($start,$end) = $time;
//                     $row['start'] = $start;
//                     $row['end'] = $end;
//                     $row['hospital_id'] = $hospitalAppoint->doctorid;
//                     $row['hospital_appoint_id'] = $hospitalAppoint->id;
//                     $row['is_deleted'] = $hospitalAppoint->interval == $tk?0:1;
//                     if ($hospitalAppoint->doctorid != 176156 && in_array($tvk,[19,20])) {
//                          $row['is_deleted'] = 1;
//                     }
//                     HospitalAppointInterval::query()->updateOrCreate(['start'=>$start,'end'=>$end,'hospital_appoint_id'=>$hospitalAppoint->id],$row);
//
//                 }
//             }
//         }
//        echo "预约街道导入\n";
//
//        $row = [];
//        $data = [];
//
//        $street_haids = HospitalAppointStreet::query()->select('haid')->whereIn('haid',$haids)->groupBy('haid')->pluck('haid');
//        foreach ($street_haids as $v){
//            $weeks = [];
//            for($i=0;$i<7;$i++){
//                $streets = HospitalAppointStreet::query()->where('haid',$v)->where('week',$i)->pluck('street');
//                $weeks['week'.$i] = $streets->toArray();
//            }
//            $row['street'] = json_encode($weeks);
//            $hospitalAppoint = HospitalAppoint::query()->where('id',$v)->first();
//            $row['hospital_id'] = $hospitalAppoint->doctorid;
//            $row['hospital_appoint_id'] = $v;
//            \App\Models\HospitalAppointStreet::query()->updateOrCreate(['hospital_appoint_id'=>$v],$row);
//
//        }
//        echo "预约时间号源导入\n";
//
//         $hospitalAppointWeek = HospitalAppointWeek::query()->whereIn('haid',$haids)->get();
//         var_dump($haids);
//         $row = [];
//         $data = [];
//         foreach($hospitalAppointWeek as $k=>$v){
//             $row['week'] = $v->week;
//             $row['num'] = $v->num;
//             $hospitalAppoint = \App\Models\HospitalAppoint::query()->where('id',$v->haid)->first();
//             if(!$hospitalAppoint)
//                 continue;
//             $row['hospital_id'] = $hospitalAppoint->hospital_id;
//             $row['hospital_appoint_id'] = $v->haid;
//
//             $ordInterval = self::$timeText1[$v->time_type];
//             $time = explode('-',$ordInterval);
//             list($start,$end) = $time;
//             $interval = HospitalAppointInterval::query()->where('hospital_appoint_id',$v->haid)
//                     ->where('start',$start)
//                     ->where('end',$end)
//                     ->first();
//
//             $row['interval_id'] = $interval->id;
//             echo implode(',',$row);
//             echo "\n";
//             HospitalAppointIntervalNum::query()->updateOrCreate(['week'=>$v->week,'interval_id'=>$interval->id,'hospital_appoint_id'=>$v->haid],$row);
//         }
//        echo "月龄设置导入\n";
//
//        $row = [];
//         $data = [];
//        $m_haids = HospitalAppoint::query()->where('type',1)->pluck('id');
//         foreach($m_haids as $hv){
//             var_dump($hv);
//             $data = [];
//             $child_types = FamilyType::query()->where('examination',1)->get();
//             foreach($child_types as $v){
//                 if(isset(self::$month[$v->id])){
//                     $oldMonth = \App\Models\old\HospitalAppointMonth::query()->where('haid',$hv)->where('type',($v->id-1))->get();
//                     $oldMonthAll = \App\Models\old\HospitalAppointMonth::query()->where('haid',$hv)->get();
//
//                     $row['type_name'] = $v->name;
//                     $row['type_id'] = $v->id;
//                     $row['is_open'] = $oldMonth->isNotEmpty() || ($oldMonthAll->isEmpty() && $v->id!=1)?1:0;
//                     $monthRs = [];
//                     foreach(self::$month[$v->id] as $mv){
//                         $oldMonth = \App\Models\old\HospitalAppointMonth::query()
//                             ->where('haid',$hv)
//                             ->where('type',($v->id-1))
//                             ->where('month',$mv['month'])
//                             ->first();
//                         $monthR = $mv;
//                         $monthR['is_check'] = isset($oldMonth)?1:0;
//                         $monthRs[] = $monthR;
//                     }
//                     $row['months'] = $monthRs;
//                     $data[] = $row;
//                 }
//             }
//             $rs['month'] = json_encode($data);
//             $rs['hospital_appoint_id'] = $hv;
//             $hospitalAppoint = \App\Models\HospitalAppoint::query()->where('id',$hv)->first();
//             if(!$hospitalAppoint)
//                 continue;
//             $rs['hospital_id'] = $hospitalAppoint->hospital_id;
//             \App\Models\HospitalAppointMonth::query()->updateOrCreate(['hospital_appoint_id'=>$hv],$rs);
//         }
        echo "疫苗设置导入\n";

        $vaccine = \App\Models\old\HospitalAppointVaccine::query()->select(['haid','vaccine'])->groupBy(['haid','vaccine'])->get();
        $in = [];
        $row = [];
        foreach ($vaccine as $v) {
            $hospitalAppoint = \App\Models\HospitalAppoint::query()->where('id', $v->haid)->first();
            if (!$hospitalAppoint)
                continue;
            $oldWeeks = \App\Models\old\HospitalAppointVaccine::query()
                ->where('haid', $v->haid)
                ->where('vaccine', $v->vaccine)
                ->pluck('week');

            $newHospitalAppointVaccine = \App\Models\HospitalAppointVaccine::query()
                ->where('hospital_appoint_id',$v->haid)
                ->where('vaccine_id',$v->vaccine)
                ->first();
            if($newHospitalAppointVaccine){
                $collection1 = collect($oldWeeks->toArray());
                $collection2 = collect(json_decode($newHospitalAppointVaccine->weeks,true));
                $weeks = array_unique($collection1->union($collection2)->toArray());
            }else{
                $weeks = $oldWeeks->toArray();
            }
            $weeks = array_unique($weeks);
            $oldHospitalAppoint = HospitalAppoint::query()->where('id', $v->haid)->first();
            if (!$oldHospitalAppoint)
                continue;

            $amPm = \App\Models\old\HospitalAppointVaccine::query()
                ->where('haid', $v->haid)
                ->where('type', 2)
                ->get();
            $is_amPm = $amPm->isNotEmpty() ? 1 : 0;


            var_dump($hospitalAppoint->id);

            if ($v->vaccine == 0) {
                $Va = \App\Models\old\Vaccine::query()->where('type', 0)->where('alltype', 0)->get();
                foreach ($Va as $vav) {
                    $row['vaccine_id'] = $vav->id;
                    $row['weeks'] = json_encode($weeks);
                    $row['week_num'] = json_encode(self::vaccineWeeks($v->haid, $vav->id));
                    $row['interval_num'] = json_encode(self::vaccineTime($vav->id, $oldHospitalAppoint->doctorid, $oldHospitalAppoint->type, $weeks, $hospitalAppoint->id));
                    $row['hospital_appoint_id'] = $v->haid;
                    $row['hospital_id'] = $hospitalAppoint->hospital_id;
                    \App\Models\HospitalAppointVaccine::query()->updateOrCreate(['hospital_appoint_id' => $v->haid, 'vaccine_id' => $vav->id], $row);
                }
            } elseif ($v->vaccine == -1) {
                $Va = \App\Models\old\Vaccine::query()->where('type', 1)->where('adult',0)->where('alltype', 0)->get();
                foreach ($Va as $vav) {

                    $row['weeks'] = json_encode($weeks);
                    $row['vaccine_id'] = $vav->id;
                    $row['hospital_appoint_id'] = $v->haid;
                    $row['hospital_id'] = $hospitalAppoint->hospital_id;
                    $row['week_num'] = json_encode(self::vaccineWeeks($v->haid, $vav->id));
                    $row['interval_num'] = json_encode(self::vaccineTime($vav->id, $oldHospitalAppoint->doctorid, $oldHospitalAppoint->type, $weeks, $hospitalAppoint->id));
                    \App\Models\HospitalAppointVaccine::query()->updateOrCreate(['hospital_appoint_id' => $v->haid, 'vaccine_id' => $vav->id], $row);
                }
            } else {
                $row['weeks'] = json_encode($weeks);
                $row['vaccine_id'] = $v->vaccine;
                $row['hospital_appoint_id'] = $v->haid;
                $row['hospital_id'] = $hospitalAppoint->hospital_id;
                $row['week_num'] = json_encode(self::vaccineWeeks($v->haid, $v->vaccine));
                $row['interval_num'] = json_encode(self::vaccineTime($v->vaccine, $oldHospitalAppoint->doctorid, $oldHospitalAppoint->type, $weeks, $hospitalAppoint->id));
                \App\Models\HospitalAppointVaccine::query()->updateOrCreate(['hospital_appoint_id' => $v->haid, 'vaccine_id' => $v->vaccine], $row);
            }
        }
        echo "儿童疫苗单独导入\n";

        $v_haids = HospitalAppoint::query()->where('type',2)->get();
        foreach ($v_haids as $v){

            $hospitalAppoint = \App\Models\HospitalAppoint::query()->where('id',$v->id)->first();
            if(!$hospitalAppoint)
                continue;
            $weeks = str_split((string)$v->weeks);

            $vaccine = \App\Models\old\HospitalAppointVaccine::query()->where('haid',$v->id)->get();
            if($vaccine->isEmpty()){
                $in = [];
                for($i=0;$i<7;$i++){

                    $w['week'.$i] = null;
                }

                $row['vaccine_id'] = 1000;
                $row['weeks'] = json_encode($weeks);
                $row['week_num'] = json_encode($w);
                $row['interval_num'] = json_encode($in);
                $row['hospital_appoint_id'] = $v->id;
                $row['hospital_id'] = $hospitalAppoint->hospital_id;
                \App\Models\HospitalAppointVaccine::query()->updateOrCreate(['hospital_appoint_id'=>$v->id,'vaccine_id'=>1000],$row);
            }
        }
        echo date('Y-m-d H:i:s')."\n";

        echo "生成排班\n";

        $hospitalAppoints = \App\Models\HospitalAppoint::query()->where('is_open', 1)->get();
        foreach ($hospitalAppoints as $hospitalAppoint) {
            try {
                AppointNum::SetupScheduling($hospitalAppoint->id);
            } catch (\Exception $e) {
                $rs = [];
                $rs[] = $hospitalAppoint->id;
                $hospital = Hospital::query()->where('id', $hospitalAppoint->hospital_id)->first();
                $rs[] = isset($hospital) ? $hospital->name : "";
                $AppointType = HospitalAppointType::query()->where('id', $hospitalAppoint->type)->first();
                $rs[] = isset($AppointType) ? $AppointType->name : "";
                $rs[] = $e->getMessage();
                $rs[] = "\n";
                echo implode(',', $rs);
            }
        }
        echo date('Y-m-d H:i:s')."\n";

        return 0;
    }

    public static function vaccineWeeks($haid, $vaccine)
    {
        for ($i = 0; $i < 7; $i++) {
            $weekNum = \App\Models\old\HospitalAppointVaccineNum::query()
                ->where('haid', $haid)
                ->where('vaccine', $vaccine)
                ->where('week',$i)
                ->first();
            $w['week' . $i] = $weekNum ? $weekNum->num : null;
        }
        return $w;
    }

    public static function vaccineTime($vaccine, $hospitalid, $type, $weeks, $haid, $amPmType = 0, $is_amPm = 0)
    {
        $hospitalAppointVaccineTimeNum = HospitalAppointVaccineTimeNum::query()
            ->where('vaccine', $vaccine)
            ->where('doctorid', $hospitalid)
            ->where('type', $type)
            ->get();
        $in = [];
        if ($is_amPm) {
            var_dump($amPmType);
            $timeText = self::$amPmArr[$amPmType];
        } else {
            $timeText = self::$timeText1;
        }
        if ($hospitalAppointVaccineTimeNum->isEmpty() && !$is_amPm) {
            return [];
        }
        var_dump($vaccine);
        var_dump($type);

        var_dump(implode(',',$timeText));
        foreach ($weeks as $week) {

            $hospitalAppointVaccine = HospitalAppointVaccine::query()
                ->where('vaccine',$vaccine)
                ->where('haid',$haid)
                ->where('week',$week)
                ->pluck('type');
            if($hospitalAppointVaccine && in_array(2,$hospitalAppointVaccine->toArray()) && in_array(1,$hospitalAppointVaccine->toArray())){
                $timeText = self::$timeText1;
            }
            if($hospitalAppointVaccine && in_array(2,$hospitalAppointVaccine->toArray()) && !in_array(1,$hospitalAppointVaccine->toArray())){
                $timeText = self::$amPmArr[2];
            }


            $hospitalAppointVaccineTimeNumWeekIsWeek = HospitalAppointVaccineTimeNum::query()
                ->where('vaccine', $vaccine)
                ->where('doctorid', $hospitalid)
                ->where('type', $type)
                ->where('week', $week)
                ->first();

            foreach ($timeText as $tk => $tv) {
                $rs['num'] = null;
                if ($hospitalAppointVaccineTimeNum->isNotEmpty()) {

                    $hospitalAppointVaccineTimeNumWeek = HospitalAppointVaccineTimeNum::query()
                        ->where('vaccine', $vaccine)
                        ->where('doctorid', $hospitalid)
                        ->where('type', $type)
                        ->where('appoint_time', $tk)
                        ->where('week', $week)
                        ->first();
                    if ($hospitalAppointVaccineTimeNumWeek) {
                        $rs['num'] = $hospitalAppointVaccineTimeNumWeek->num;
                    }
                }

                $rs['week'] = $week;
                $ordInterval = self::$timeText1[$tk];
                $time = explode('-', $ordInterval);
                list($start, $end) = $time;
                $interval = \App\Models\HospitalAppointInterval::query()->where('hospital_appoint_id', $haid)
                    ->where('start', $start)
                    ->where('end', $end)
                    ->first();
                if (!isset($interval)) {
                    continue;
                }
                if(!$hospitalAppointVaccineTimeNumWeekIsWeek) {
                    $intervalNum = HospitalAppointIntervalNum::query()
                        ->where('hospital_appoint_id', $haid)
                        ->where('interval_id', $interval->id)
                        ->where('week', $week)
                        ->first();
                    if (isset($intervalNum) && $intervalNum->num > 0) {
                        $rs['open'] = 1;
                        $rs['interval_id'] = $interval->id;
                    } else {
                        continue;
                    }
                }else{
                    if(!$hospitalAppointVaccineTimeNumWeek){
                        continue;
                    }
                    $rs['open'] = 1;
                    $rs['interval_id'] = $interval->id;
                }
                $in[] = $rs;
            }
        }

        return $in;
    }
}
